//
// PreviewPane.java
//
package com.photoexpert;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import javax.swing.JPanel;

class PreviewPane extends JPanel implements MouseListener, MouseMotionListener {

    static PreviewPane CreateInstance() {
        PreviewPane instance = new PreviewPane();
        instance.addMouseListener(instance);
        instance.addMouseMotionListener(instance);
        return instance;
    }

    void setOp(BufferedImageOp op) {
        _op = op;
        if(_op == null)
            _image = _srcImage;
        else
            _image = _op.filter(_srcImage, null);
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        int maxX = _implementor.getWidth() - 250;
        int maxY = _implementor.getHeight() - 190;
        _curX = _originalX + _downX - e.getX();
        _curY = _originalY + _downY - e.getY();
        if(_curX > maxX) _curX = maxX;
        if(_curX < 0) _curX = 0;
        if(_curY > maxY) _curY = maxY;
        if(_curY < 0) _curY = 0;
        Graphics g = _srcImage.createGraphics();
        g.drawImage(_implementor.getImage().getSubimage(_curX, _curY, 250, 190), 0, 0, null);
        g.dispose();
        setOp(_op);
    }

    @Override
    public void mouseMoved(MouseEvent e) { }
    @Override
    public void mouseClicked(MouseEvent e) { }
    @Override
    public void mousePressed(MouseEvent e) {
        _downX = e.getX();
        _downY = e.getY();
        _originalX = _curX;
        _originalY = _curY;
    }
    @Override
    public void mouseReleased(MouseEvent e) { }
    @Override
    public void mouseEntered(MouseEvent e) { }
    @Override
    public void mouseExited(MouseEvent e) { }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(_image, 1, 1, null);
    }

    /* ----------------------------------------------------------------------- */

    private PreviewPane() {
        setCursor(Cursors._moveCursor);
        int type = (_implementor.getImage().getTransparency() == Transparency.OPAQUE) ?
            BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
        _srcImage = new BufferedImage(250, 190, type);
        Graphics g = _srcImage.createGraphics();
        if(_curX + 250 > _implementor.getWidth())
            _curX = 0;
        if(_curY + 190 > _implementor.getHeight())
            _curY = 0;
        g.drawImage(_implementor.getImage().getSubimage(_curX, _curY, 250, 190), 0, 0, null);
        g.dispose();
        _image = _srcImage;
    }

    private static Implementor  _implementor = Implementor.Instance();
    private static int          _curX = 0;
    private static int          _curY = 0;
    private BufferedImage       _srcImage = null;
    private BufferedImage       _image = null;
    private BufferedImageOp     _op = null;
    // variables used in mouse movement
    private int                 _downX = 0;
    private int                 _downY = 0;
    private int                 _originalX = 0;
    private int                 _originalY = 0;
}
